package com.google.gwt.sample.dynatable.server;

import com.google.gwt.sample.dynatable.client.City;
import com.google.gwt.sample.dynatable.client.Country;
import com.google.gwt.sample.dynatable.client.LocationService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: denis_zavadsky
 * Date: 9/14/13
 * Time: 11:05 AM
 * To change this template use File | Settings | File Templates.
 */
public class LocationServiceImpl extends RemoteServiceServlet implements LocationService {

    private static City[] citiesUS = new City[]{
            new City(1L,"New-York",1L),
            new City(2L,"Washington",1L),
            new City(3L,"San-Francisco",1L)
            };

    private static  City[] citiesRU = new City[]{
            new City(4L,"Moscow",2L),
            new City(5L,"Saratov",2L),
            new City(6L,"Rostov",2L),
    };


    private static Country[] countries = new Country[]{
            new Country(1L,"United States", Arrays.asList(citiesUS)),
            new Country(2L,"Russian Federation", Arrays.asList(citiesRU))
    };

    private static List<City> allCities = new ArrayList<City>();


    @Override
    public List<Country> getAllCountries() {
        return Arrays.asList(countries);
    }

    @Override
    public List<City> getCityByCountryId(Long countryId) {
        allCities.clear();
        allCities.addAll(Arrays.asList(citiesRU));
        allCities.addAll(Arrays.asList(citiesUS));
        List<City> result = new ArrayList<City>();
        for (City city : allCities){
            if (city.getCountryId().equals(countryId)){
                result.add(city);
            }
        }
        return result;  //To change body of implemented methods use File | Settings | File Templates.
    }


}
